from typing import Union, TypeVar, Generic

from fastapi import status
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel


T = TypeVar('T', bound=BaseModel)


def log_response(func):
    '''Response响应规范，为了能记录所有的自定义响应，所有的Response皆需要加上此装饰函数
    '''
    def wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        print("API Response:", repr(response))
        return response
    return wrapper


'''
class OkResponse(BaseModel, Generic[T]):
    code: int = 200
    msg: str = "ok"
    data: T | list | dict | None = None
    #  data: Union[list, dict,T]
'''
@log_response
class OkResponse(BaseModel):
    code: int = 200
    msg: str = "ok"
    data: T | list | dict | None = None
    #  data: Union[list, dict,T]


@log_response
class ServerErrorResponse(BaseModel):
    code: int = 500
    msg: str = "server error"
    data: Union[list, dict, str, None] = None


@log_response
class BadRequestResponse(BaseModel):
    code: int = 400
    msg: str = "bad request"
    data: Union[list, dict, str, None] = None


@log_response
class OkDuplicateAddResponse(BaseModel):
    """when execute API for an add request, if the record
    has existed in database, then use this response"""
    code : int = 201
    msg: str = "ok_already_exist"
    data: Union[list, dict, str, None] = None

@log_response
class OkMissDomainUrlResponse(BaseModel):
    """when execute API for /node/refresh request, if the 
    domain url is not set, then use this response"""
    code : int = 202
    msg: str = "ok_miss_domain_url"
    data: Union[list, dict, str, None] = None